﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MediaRecommender.Model;
using MediaRecommender.Controller;

namespace MediaRecommender
{


    public partial class Medialist : System.Web.UI.Page
    {
        protected string PageTitle = "Medialist";

        private string emptyCover = "http://thumbs.dreamstime.com/m/book-empty-cover-white-28912004.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            User u = UserCollection.Instance.GetUser((string)Session["Username"]);
            int age = u.Age;

            #region generatePictures
            foreach (Media m in u.MediaList.Keys)
            {
                //construct table cell
                ImageButton imgButton = new ImageButton();
                TextBox textbox = new TextBox();
                TableCell td = new TableCell();
                Image ratingImg = new Image();

                ratingImg.Width = 214;


                int rating = 0;
                if (u.MediaList.TryGetValue(m, out rating))
                {
                    ratingImg.ImageUrl = "Pics/Rating/" + Convert.ToString(rating) +".png";
                }

                imgButton.Attributes["style"] = "float:buttom";
                textbox.Attributes["style"] = "position:relative";
                textbox.Attributes["style"] = "float:top";
                textbox.Width = 214;
                textbox.Attributes["style"] = "background-color:#e0ffff";
                textbox.ReadOnly = true;

                if (m.CoverUrl.Length != 0 && m.CoverUrl != null)
                {
                    imgButton.ImageUrl = m.CoverUrl;
                }
                else imgButton.ImageUrl = emptyCover;

                textbox.Text = m.Title; td.Text = Convert.ToString(m.ID);
                imgButton.OnClientClick = "return confirm('Are you sure you want to remove this media?');";
                imgButton.Width = 214;
                imgButton.Height = 350;
                imgButton.Command += Image_Click;
                imgButton.ToolTip = showInfo(m);
                td.Controls.Add(textbox);
                td.Controls.Add(imgButton);

                
                ratingImg.Style["position"] = "relative";
                ratingImg.Style["left"] = "0px";
                ratingImg.Style["top"] = "-46px";

                td.Controls.Add(ratingImg);
                //

                //determain wether or not a new line is needed and then adds it and the cell or just the cell
                TableRowCollection trList = TableHeader.Rows;
                int count = trList.Count; //needed to acces the last row

                if (trList[count - 1].Cells.Count > 3)
                {
                    TableRow tr = new TableRow();
                    tr.Cells.Add(td);
                    TableHeader.Rows.Add(tr);
                }
                else
                {
                    trList[count - 1].Cells.Add(td);
                }

            }
            #endregion

        }

        private string showInfo(Media m)
        {
            string infoToReturn = "";

            infoToReturn += "Genre: ";
            foreach (Genre g in m.Genres)
            {
                infoToReturn += Enum.GetName(typeof(Genre), g) + ", ";
            }

            if (m is VideoGame)
            {
                VideoGame VG = (VideoGame)m;
                infoToReturn += "\nPlatforms: ";
                foreach (Platform p in VG.Platforms)
                    infoToReturn += Enum.GetName(typeof(Platform), p) + ", ";
                infoToReturn += "\nPublisher: " + VG.Publisher.Name;
                infoToReturn += "\nStudio: " + VG.Studio.Name;
            }
            else if (m is Movie)
            {
                Movie MO = (Movie)m;
                int maxActors = 5;
                int count = 0;
                infoToReturn += "\nActors: ";
                foreach (Actor a in MO.Actors)
                {
                    infoToReturn += a.Name + ", ";
                    count++;
                    if (maxActors < count) break;
                }
                infoToReturn += "\nDirector: " + MO.Director.Name;
            }
            else
            {
                Book B = (Book)m;
            }
            return infoToReturn;
        }

        protected void Image_Click(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            TableCell td = (TableCell)img.Parent;
            User u = UserCollection.Instance.GetUser((string)Session["Username"]);
            MedialistController mc = new MedialistController(u);
            Media m = MediaCollection.Instance.GetMedia(x => x.ID == Convert.ToUInt32(td.Text))[0];

            mc.RemoveMedia(m);
            Response.Redirect("Medialist.aspx");
        }


    }
}
